package com.yehuishou.util;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.aes.AHEEHES;

public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer {
	
	//"driverClassName", "url", "username", "password"
	private String[] encryptPropNames = { "jdbc.url" , "jdbc.password" };

	@Override
	protected String convertProperty(String propertyName, String propertyValue) {

		// 如果在加密属性名单中发现该属性
		if (isEncryptProp(propertyName)) {
			String decryptValue;
			try {
				decryptValue = AHEEHES.Decrypt(propertyValue);
			} catch (Exception e) {
				return propertyValue;
			}
			return decryptValue;
		} else {
			return propertyValue;
		}
	}

	private boolean isEncryptProp(String propertyName) {
		for (String encryptName : encryptPropNames) {
			if (encryptName.equals(propertyName)) {
				return true;
			}
		}
		return false;
	}
}
